﻿using System;
using System.Net;
using System.Net.Browser;
using System.ServiceModel;
using System.Windows;

namespace TaskClient4
{
    public partial class App : Application
    {
        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;
            WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = new MainPage();
        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            const string msgFormat = "Message:{0} {1} StackTrace:{2}";
            var msg = e.ExceptionObject.Message;
            var stackTrace = e.ExceptionObject.StackTrace;
            if (e.ExceptionObject.InnerException != null && e.ExceptionObject.InnerException is FaultException<ExceptionDetail>)
            {
                var ex = e.ExceptionObject.InnerException as FaultException<ExceptionDetail>;
                msg = ex.Detail.Message;
                stackTrace = ex.Detail.StackTrace;
            }
            Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(string.Format(msgFormat, msg, System.Environment.NewLine, stackTrace)));
            e.Handled = true;
        }
    }
}
